#include "SysApiManager.h"
#include <stdlib.h>
#include <jni.h>
#include "platform/android/jni/JniHelper.h"

USING_NS_CC;

extern "C" {

	static void startWaiting_c()
	{
		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/SysApiManager", "startWaiting", "()V")) {

			t.env->CallStaticVoidMethod(t.classID, t.methodID);

			t.env->DeleteLocalRef(t.classID);
		}
	}

	static void finishWaiting_c()
	{
		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/SysApiManager", "finishWaiting", "()V")) {

			t.env->CallStaticVoidMethod(t.classID, t.methodID);

			t.env->DeleteLocalRef(t.classID);
		}
	}

	static void showLoading(const char * pszMsg, const char * pszTitle) {
		if (!pszMsg) {
			return;
		}

		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/SysApiManager", "startLoading", "(Ljava/lang/String;Ljava/lang/String;)V")) {
			jstring stringArg1;

			if (!pszTitle) {
				stringArg1 = t.env->NewStringUTF("");
			} else {
				stringArg1 = t.env->NewStringUTF(pszTitle);
			}

			jstring stringArg2 = t.env->NewStringUTF(pszMsg);
			t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg2, stringArg1);

			t.env->DeleteLocalRef(stringArg1);
			t.env->DeleteLocalRef(stringArg2);
			t.env->DeleteLocalRef(t.classID);
		}
	}
	static void hideLoading() {
		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/SysApiManager", "finishLoading", "()V")) {
			
			t.env->CallStaticVoidMethod(t.classID, t.methodID);

			t.env->DeleteLocalRef(t.classID);
		}
	}

	static void showTips_c(const char* tips)
	{
		if (!tips) {
			return;
		}

		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/SysApiManager", "showTips", "(Ljava/lang/String;)V")) {

			jstring stringArg1 = t.env->NewStringUTF(tips);

			t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1);

			t.env->DeleteLocalRef(stringArg1);
			t.env->DeleteLocalRef(t.classID);
		}
	}

	static void showExitDialog_c()
	{
		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/SysApiManager", "showExitDialog", "()V")) {

			t.env->CallStaticVoidMethod(t.classID, t.methodID);

			t.env->DeleteLocalRef(t.classID);
		}
	}
	static void sendWxReqMsg_c(const char* msg, const char* desc)
	{
		if (!msg) {
			return;
		}

		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/SysApiManager", "sendWxReqMsg", "(Ljava/lang/String;Ljava/lang/String;)V")) {

			jstring stringArg1 = t.env->NewStringUTF(msg);

			jstring stringArg2;
			if (!desc) {
				stringArg2 = t.env->NewStringUTF("");
			} else {
				stringArg2 = t.env->NewStringUTF(desc);
			}

			t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, stringArg2);

			t.env->DeleteLocalRef(stringArg1);
			t.env->DeleteLocalRef(stringArg2);
			t.env->DeleteLocalRef(t.classID);
		}
	}

	static void sendWxReqImage_c(const char* filename, const char* desc)
	{
		if (!filename) {
			return;
		}

		JniMethodInfo t;
		if (JniHelper::getStaticMethodInfo(t, "org/gzy/xiaolian/SysApiManager", "sendWxReqImage", "(Ljava/lang/String;Ljava/lang/String;)V")) {

			jstring stringArg1 = t.env->NewStringUTF(filename);

			jstring stringArg2;
			if (!desc) {
				stringArg2 = t.env->NewStringUTF("");
			} else {
				stringArg2 = t.env->NewStringUTF(desc);
			}

			t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, stringArg2);

			t.env->DeleteLocalRef(stringArg1);
			t.env->DeleteLocalRef(stringArg2);
			t.env->DeleteLocalRef(t.classID);
		}
	}
}

SysApiManager* SysApiManager::sharedSysApiManager()
{
	static SysApiManager s_SysApiManager;
	return &s_SysApiManager;
}

void SysApiManager::startWaiting()
{
	startWaiting_c();
}

void SysApiManager::finishWaiting()
{
	finishWaiting_c();
}

void SysApiManager::startLoading(const char* msg, const char* title)
{
	showLoading(msg, title);
}

void SysApiManager::finishLoading()
{
	hideLoading();
}

void SysApiManager::showTip(const char* tips)
{
	showTips_c(tips);
}

void SysApiManager::showExitDialog()
{
	showExitDialog_c();
}

void SysApiManager::sendWxReqMsg(const char* msg, const char* desc)
{
	sendWxReqMsg_c(msg, desc);
}

void SysApiManager::sendWxReqImage(const char* filename, const char* desc)
{
	sendWxReqImage_c(filename, desc);
}